//
//  ByCellProductCommentListItem.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/11/2.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByCellProductCommentListItem.h"

// view
#import "ByViewVideoPlayer.h"

@implementation ByCellProductCommentListItem
{
    UIImageView *_iconView;             // 头像
    UILabel     *_nicknameLabel;        // 昵称
    UILabel     *_rankTagLabel;         // 评价级别标签
    UILabel     *_contentLabel;         // 内容
    UILabel     *_skuDescLabel;         // 规格描述
    UILabel     *_dateLabel;            // 日期
    
    ByViewVideoPlayer *_videoView;      // 播放视图
    
    UIView      *_line;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    // 头像
    _iconView = [UIImageView new];
    _iconView.layer.cornerRadius = 18;
    _iconView.layer.masksToBounds = true;
    _iconView.image = [UIImage imageWithColor:[UIColor redColor]];
    [self.contentView addSubview:_iconView];
    
    // 昵称
    _nicknameLabel = [[UILabel alloc] init];
    _nicknameLabel.text = @"昵称";
    _nicknameLabel.font = FONT(15);
    _nicknameLabel.textAlignment = NSTextAlignmentLeft;
    _nicknameLabel.textColor = [UIColor blackColor];
    [self.contentView addSubview:_nicknameLabel];
    
    // 视频标签
    _rankTagLabel = [[UILabel alloc] init];
    _rankTagLabel.font = FONT(12);
    _rankTagLabel.textAlignment = NSTextAlignmentCenter;
    _rankTagLabel.textColor = BY_BLUE_COLOR;
    _rankTagLabel.layer.cornerRadius = 2;
    _rankTagLabel.layer.borderWidth = 1;
    [self.contentView addSubview:_rankTagLabel];
    
    // 内容
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.numberOfLines = 0;
    _contentLabel.text = @"评价内容";
    _contentLabel.font = FONT(14);
    _contentLabel.textAlignment = NSTextAlignmentLeft;
    _contentLabel.textColor = [UIColor blackColor];
    [self.contentView addSubview:_contentLabel];
    
    // 视频
    _videoView = [ByViewVideoPlayer new];
    [self.contentView addSubview:_videoView];
    
    // 规格
    _skuDescLabel = [[UILabel alloc] init];
    _skuDescLabel.text = @"商品规格";
    _skuDescLabel.font = FONT(14);
    _skuDescLabel.textAlignment = NSTextAlignmentLeft;
    _skuDescLabel.textColor = [UIColor lightGrayColor];
    [self.contentView addSubview:_skuDescLabel];
    
    // 日期
    _dateLabel = [[UILabel alloc] init];
    _dateLabel.text = @"日期";
    _dateLabel.font = FONT(14);
    _dateLabel.textAlignment = NSTextAlignmentRight;
    _dateLabel.textColor = [UIColor lightGrayColor];
    [self.contentView addSubview:_dateLabel];
    
    // 布局
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.top.equalTo(@15);
        make.size.mas_equalTo(CGSizeMake(36, 36));
    }];
    [_nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_iconView.mas_right).offset(10);
        make.centerY.equalTo(_iconView);
    }];
    [_rankTagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-10);
        make.size.mas_equalTo(CGSizeMake(34, 18));
        make.centerY.equalTo(_iconView);
    }];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nicknameLabel);
        make.right.equalTo(@-10);
        make.top.equalTo(_iconView.mas_bottom);
    }];
    [_videoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.right.equalTo(@-10);
        make.height.equalTo(_videoView.mas_width).multipliedBy(0.5);
        make.top.equalTo(_contentLabel.mas_bottom).offset(10);
    }];
    [_skuDescLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_contentLabel);
        make.height.equalTo(@15);
    }];
    [_dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_contentLabel);
        make.top.equalTo(_skuDescLabel.mas_top);
        make.height.equalTo(_skuDescLabel);
    }];
}

- (void)setModel:(ByModelProductComment *)model {
    
    _model = model;
    
    _nicknameLabel.text = model.nickname;
    switch (model.rank) {
        case ByProductCommentRankGood:
            _rankTagLabel.textColor = [UIColor greenColor];
            _rankTagLabel.text = @"好评";
            break;
        case ByProductCommentRankMid:
            _rankTagLabel.textColor = BY_GOLD_COLOR;
            _rankTagLabel.text = @"中评";
            break;
        case ByProductCommentRankBad:
            _rankTagLabel.textColor = BY_RED_COLOR;
            _rankTagLabel.text = @"差评";
            break;
        default:
            break;
    }
    _rankTagLabel.layer.borderColor = _rankTagLabel.textColor.CGColor;

    _contentLabel.text = model.content;
    _skuDescLabel.text = model.skuDesc;
    _dateLabel.text = model.date;
    
    if (model.hasVideo) {
        _videoView.hidden = false;
        [_contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView).offset(-(10 + (SCREEN_WIDTH - 20) / 2 + 10 + 15 + 10));
        }];
    } else {
        _videoView.hidden = true;
        [_contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView).offset(-(10 + 15 + 10));
        }];
    }
    [_skuDescLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-10);
    }];
}

@end
